package com.sunny.myproject.system.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sunny.myproject.core.base.action.BaseAction;
import com.sunny.myproject.system.entity.Menu;
import com.sunny.myproject.system.service.MenuService;

@Controller
@RequestMapping("/system/menu")
public class MenuAction extends BaseAction<Menu>{

	private Logger logger = Logger.getLogger(MenuAction.class);
	@Autowired
	private MenuService service;
	
	@Override
	@RequestMapping(params="list")
	public String list(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		super.pager = super.pageStart(model, request);
		super.pager = super.pageEnd(model, service.num(criterions), pager);
		super.returnValue(model, service.find(criterions, super.pager.getFirstResults(), super.pager.getMaxResults()));
		return "system/menuList";
	}

	@Override
	@RequestMapping(params="add")
	public String add(HttpServletRequest reuqest, HttpServletResponse response) {
		return "system/menuEdit";
	}

	@Override
	@RequestMapping(params="edit")
	public String edit(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			super.returnValue(model, service.findById(super.param1(request)));
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		return "system/menuEdit";
	}

	@Override
	@RequestMapping(params="delete")
	public String delete(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			service.deleteByIds(super.param2(request));
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		return "redirect:/system/menu?list";
	}

	@Override
	@RequestMapping(params="save")
	public @ResponseBody String save(HttpServletRequest request,
			HttpServletResponse response, @ModelAttribute Menu t) {
		try {
			if(t.getFather().getId()==null)
				t.setFather(null);
			service.save(t);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		return "system/menu?list";
	}

	@Override
	@RequestMapping(params="view")
	public String view(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			super.returnViewValue(model, service.findById(super.param1(request)));
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		return "system/menuEdit";
	}

	@RequestMapping(params="query")
	public String query(Model model,HttpServletRequest request,HttpServletResponse response){
		super.pager = super.pageStart(model, request);

		criterions.add(Restrictions.eq("top", "1"));
		super.pager = super.pageEnd(model, service.num(criterions), pager);
		super.returnValue(model, service.find(criterions, super.pager.getFirstResults(), super.pager.getMaxResults()));
		return "system/menuQuery";
	}
}
